package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.adapter.DeviceSelectionAdapter;
import com.dotstone.chipism.adapter.SceneAdapter;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.Scene;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.ShareManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.BlurBehind;
import com.dotstone.chipism.view.OnBlurCompleteListener;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShareEditActivity extends BaseActivity {
	private TextView tv_statusBar_mian;
	private LinearLayout mBackL;
	private Button mNextB;
	private TextView mEnvT;
	private TextView mDeviceT;
	private ListView mEnvL;
	private ListView mDeviceL;
	// private ListView mWlanL;
	private LinearLayout mDeviceLayout;
	private List<Scene> mScenes = new ArrayList<Scene>();
	private List<Scene> mSelects = new ArrayList<Scene>();
	private SceneAdapter mAdapter;
	private List<Device> mDevices = new ArrayList<Device>();
	private List<Device> mWlans = new ArrayList<Device>();
	private DeviceSelectionAdapter mAdapter1;
	// private DeviceSelectionAdapter mAdapter2;
	private List<Device> mDeviceSelects = new ArrayList<Device>();

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.env_tv:
			mEnvL.setVisibility(View.VISIBLE);
			mDeviceLayout.setVisibility(View.GONE);
			mDeviceT.setEnabled(true);
			mEnvT.setEnabled(false);
			mEnvT.setBackgroundResource(R.drawable.bg_white_conner_stroke);
			mDeviceT.setBackgroundResource(R.color.transparent);
			break;
		case R.id.device_tv:
			mDeviceLayout.setVisibility(View.VISIBLE);
			mEnvL.setVisibility(View.GONE);
			mDeviceT.setEnabled(false);
			mEnvT.setEnabled(true);
			mDeviceT.setBackgroundResource(R.drawable.bg_white_conner_stroke);
			mEnvT.setBackgroundResource(R.color.transparent);
			break;
		case R.id.nextstep:
			// 获取已选择的情景
			for (int i = 0; i < mScenes.size(); i++) {
				if (mScenes.get(i).isSelect()) {
					mSelects.add(mScenes.get(i));
					Log.i("wmy", "添加场景" + mScenes.get(i).getSceneName());
				}
			}
			// 获取已选择的设备
			for (int i = 0; i < mDevices.size(); i++) {
				if (mDevices.get(i).isSelected()) {
					mDeviceSelects.add(mDevices.get(i));
					Log.i("wmy", "已选择" + mDevices.get(i).getDeviceName());
				}
			}
			// 根据情景 将情景里面的设备也添加到已选择的设备中
			for (int i = 0; i < mSelects.size(); i++) {
				List<Device> devices = mSelects.get(i).getDevices();
				for (int j = 0; j < devices.size(); j++) {
					boolean e = false;
					for (Device d : mDeviceSelects) {
						if (d.getDeviceID().equals(devices.get(j).getDeviceID())) {
							e = true;
						}
					}
					if (!e) {
						mDeviceSelects.add(devices.get(j));
					}
				}
			}

			// 根据设备 将设备的主网关也添加进来
			for (int i = 0; i < mDeviceSelects.size(); i++) {
				String mid = mDeviceSelects.get(i).getMainDeviceId();
				if (mid != null) {
					Device wlan = DeviceManager.getInstance().getDeviceById(mid);
					if (wlan != null) {
						boolean e = false;
						for (Device d : mDeviceSelects) {
							if (d.getDeviceID().equals(wlan.getDeviceID())) {
								e = true;
							}
						}
						if (!e) {
							mDeviceSelects.add(wlan);
						}
					}
				}
			}

			if ((mSelects.size() == 0 && mDeviceSelects.size() == 0)) {
				ToastShow.Show(getApplicationContext(), getString(R.string.share_no_scene_device));
				return;
			}
			ShareManager.getInstance().setmScenes(mSelects);
			ShareManager.getInstance().setmDevices(mDeviceSelects);
			BlurBehind.getInstance().execute(MainActivity.mainActivity, new OnBlurCompleteListener() {
				@Override
				public void onBlurComplete() {
					Intent intent = new Intent(ShareEditActivity.this, AuthorConfirmActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);
					finish();
				}
			});
			break;

		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_share_edit;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		BlurBehind.getInstance().setBackground(this);
		tv_statusBar_mian = $(R.id.tv_statusBar_mian);
		mBackL = $(R.id.back_layout);
		mNextB = $(R.id.nextstep);
		mNextB.setVisibility(View.VISIBLE);
		mEnvL = $(R.id.env_lv);
		mDeviceL = $(R.id.device_lv);
		// mWlanL = $(R.id.wlan_lv);
		mEnvT = $(R.id.env_tv);
		mDeviceT = $(R.id.device_tv);
		mDeviceLayout = $(R.id.device_l);
		boolVersion();
		mBackL.setOnClickListener(this);
		mNextB.setOnClickListener(this);
		mEnvT.setOnClickListener(this);
		mDeviceT.setOnClickListener(this);
		mEnvL.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int n, long arg3) {
				// TODO Auto-generated method stub
				mScenes.get(n).setSelect(!mScenes.get(n).isSelect());
				mAdapter.notifyDataSetChanged();
			}
		});
		mDeviceL.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				mDevices.get(position).setSelected(!mDevices.get(position).isSelected());
				mAdapter1.notifyDataSetChanged();
			}
		});
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		mScenes = DeviceManager.getInstance().getShareableScenes();
		for (int i = 0; i < mScenes.size(); i++) {
			mScenes.get(i).setSelect(false);
		}
		mAdapter = new SceneAdapter(getApplicationContext(), mScenes);
		mEnvL.setAdapter(mAdapter);
		mDevices = DeviceManager.getInstance().getShareableDevices();
		for (int i = 0; i < mDevices.size(); i++) {
			mDevices.get(i).setSelected(false);
		}
		mWlans = DeviceManager.getInstance().getWlans();
		mAdapter1 = new DeviceSelectionAdapter(getApplicationContext(), mDevices);
		mDeviceL.setAdapter(mAdapter1);
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			tv_statusBar_mian.setLayoutParams(layoutParams1);
			tv_statusBar_mian.setVisibility(View.VISIBLE);
		} else {
			tv_statusBar_mian.setVisibility(View.GONE);
		}
	}

}
